import requests
from lxml import etree


def main():
    headers = {
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
    }
    url = "https://s.weibo.com/top/summary"
    r = requests.get(url, headers=headers)
    r.encoding = r.apparent_encoding
    # with open('test.html', 'w', encoding='utf-8') as f:
    #     f.write(r.text)
    response = etree.HTML(r.text)
    items = response.xpath('//ul[@class="list_a"]/li')
    r_value = int(str(response.xpath('//ul[@class="list_a"]/li[2]/a/span/em/text()')[0]))
    for item in items[1:]:
        index = int(str(item.xpath('./a/span/em/text()')[0]))
        data = {
            "rank": item.xpath('./a/strong/text()')[0],
            "keyword": item.xpath('./a/span/text()')[0],
            "index": index,
            "hot": str(int(index / r_value * 100)) + '%'
        }
        print(data)


if __name__ == "__main__":
    main()
